﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace Doo.WebUI.SiteBuilder.PropertyEditor
{
    internal class EnumEditorHelper
    {
        internal static Hashtable EnumCache = new Hashtable();

        internal static Dictionary<string, string> getDicts(Type enumType)
        {
            Dictionary<string, string> od = EnumCache[enumType] as Dictionary<string, string>;
            if (od == null)
            {
                var fields = enumType.GetFields();
                od = new Dictionary<string, string>();
                foreach (var f in fields)
                {
                    if (f.Name == "value__") continue;
                    od.Add(f.Name, ((int)f.GetValue(null)).ToString());
                }
                EnumCache[enumType] = od;
            }
            return od;
        }
    }

    public class EnumEditor<TEnum> : DropDownEditorBase
    {
        public override string ClientType
        {
            get { return "Doo.PageBuilder.PropertyEditor.EnumEditor"; }
        }

        protected override Dictionary<string, string> GetDropItems()
        {
            return EnumEditorHelper.getDicts(typeof(TEnum));
        }
    }
}
